<?php
namespace app\admin\controller;

use app\common\model\Role;
use app\common\model\Roleauthorities;


/**
 * 角色管理
 * Class Realdata
 * @package app\gygt\controller
 */
class Sysrole extends Admin
{
    /**
     * 获取列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function lst(){
        $search_word = input('keyword','','trim');
        $param=[];
        if($search_word){
            $param['role_name|comments'] =['like','%'.$search_word.'%'];
        }
        $model =new Role();
        $count =  $model->where($param)->count();
        $lst = $model->where($param)->order('role_id asc')->select();

        return json_tab_msg($count,1,$lst);
    }


    /**
     * 添加修改角色
     * @return \think\response\Json
     */
    public function add_or_save(){
        $data['role_id'] = input('role_id');
        $data['role_name'] = input('role_name','','trim');
        $data['comments'] = input('comments','','trim');

        $model =new Role();
        //验证用户是否存在
        if(!$data['role_id']){
            $result = $this->validate($data, 'SysRole.add');
            if ($result !== true) return json_msg($result);

            $data['create_time'] = date('Y-m-d H:i:s');
            $res = $model->insert($data);
            $msg = '添加成功';
        }else{
            $result = $this->validate($data, 'SysRole.update');
            if ($result !== true) return json_msg($result);

            $data['update_time'] = date('Y-m-d H:i:s');
            $res = $model->save($data,['role_id'=>$data['role_id']]);
            $msg = '修改成功';
        }

        json_success_msg($msg);
    }

    /**
     *删除角色
     */
    public function del(){
        $id = input('role_id');
        $model = new Role();
        //1 参数验证是否合法
        if (empty($id)){
            json_error_msg("角色ID不能为空！请检查");
        }
        // 超管不能删除
        if ($id == 1){
            json_error_msg("该角色不能删除");
        }
        $con['role_id'] = $id;
        $res = $model->where($con)->delete();
        return json_msg('删除成功',true);
    }


    /**
     * 获取权限详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail(){
        $id = input('role_id');
        $model =new Role();
        //1 参数验证是否合法
        if (empty($id)){
            json_error_msg("ID不能为空！请检查");
        }
        $con['role_id'] = $id;
        $res = $model->where($con)->find();
        json_success_msg($res);
    }

    /**
     * 获取全部角色
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAllList(){
        $model = new Role();
        $list = $model->field('role_id,role_name')->select();
        json_success_msg($list);
    }

    /**
     * 角色权限修改
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function modifyRoleMenu(){
        $data['role_id'] = input('role_id');
        $data['menu_id'] = input('menu_id');
        $flag = input('flag');
        if(empty($data['role_id'])) json_error_msg('角色ID不能为空，请确认');
        if(empty($data['menu_id'])) json_error_msg('菜单ID不能为空，请确认');
        $model = new Roleauthorities();
        if(!$flag){
            $model->where($data)->delete();
        }else{
            $info = $model->where($data)->find();
            if($info) json_error_msg('已绑定，请勿重复绑定');
            $data['create_time'] = date('Y-m-d H:i:s');
            $model->insert($data);
        }
        json_success_msg('操作成功');
    }
}
